package com.xju.atpgs.controller;

import com.xju.atpgs.entity.User;
import com.xju.atpgs.service.UserService;
import com.xju.atpgs.utils.SecurityUserHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = {"/","/index","/user-profile"})
    public String userProfile(Model model){
        UserDetails userDetails=(UserDetails) SecurityUserHelper.getCurrentUser();
        User user=userService.findByEmail(userDetails.getUsername());
        model.addAttribute("user",user);
        return "module_one/profile";
    }

    @RequestMapping("/save-profile")
    public String saveProfile(String id,String name,String password,String email,String profile){
        userService.saveProfile(id,name,password,email,profile);
        if(password!=null){
            return "redirect:/login";
        }
        return "redirect:/user-profile";
    }
}
